#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project ：kuberCloudTest 
@File ：utils.py
@Author ：admin
@Date ：2024/3/21 14:20 
"""
from enum import unique, Enum

from kuberCloudTest.common.basic import basicUtils
from kuberCloudTest.exception import KuberCloudException


class specUtils(basicUtils):

    def __init__(self, accessToken):
        super().__init__(accessToken)


@unique
class DiskUnitEnum(Enum):
    MBs = 'MB/s'
    GBs = 'GB/s'


@unique
class NetworkUnitEnum(Enum):
    Mbps = 'Mbps'
    Gbps = 'GBps'


@unique
class SpecType(Enum):
    DISK_SPEC = 1
    COMPUTE_SPEC = 2


def checkSpecType(specType):
    """
    check specType
    :param specType: :param specType: 1: diskSpec 2: computeSpec
    :return:
    """
    if specType not in (SpecType.DISK_SPEC.value, SpecType.COMPUTE_SPEC.value):
        raise KuberCloudException('params error', 'specType error')


class DiskIOps:
    """
        磁盘读写速度
            1：总速度
            2：读写速度
    """
    _DISK_IO_PS_TYPE = (1, 2)

    def __init__(self, diskIOpsType: int,
                 diskTotalIOps: str = '', diskReadIOps: str = '', diskWriteIOps: str = ''):
        numbs_dict = {'diskTotalIOps': diskTotalIOps, 'diskReadIOps': diskReadIOps, 'diskWriteIOps': diskWriteIOps}
        for numb_key, numb_value in numbs_dict.items():
            if not numb_value:
                continue
            try:
                int(numb_value)
            except ValueError:
                raise KuberCloudException('params error', '{} is wrong'.format(numb_key))

        self.diskIOpsType = diskIOpsType
        self.diskTotalIOps = int(diskTotalIOps) if diskTotalIOps else diskTotalIOps
        self.diskReadIOps = int(diskReadIOps) if diskReadIOps else diskReadIOps
        self.diskWriteIOps = int(diskWriteIOps) if diskWriteIOps else diskWriteIOps

        if self.diskIOpsType not in self._DISK_IO_PS_TYPE:
            raise KuberCloudException('params error', 'diskIOpsType is wrong')


class PhysicalAllocationPolicy:
    """
        物理机分配策略
            1: 运行云主机数量最少
            2: CPU使用率最低
            3: 内存使用率最低
            4: 运行云主机最大数量
    """
    _PHYSICAL_ALLOCATION_POLICY_TYPE = (1, 2, 3, 4)
    """
        strategicMode: 策略模式 (allocationPolicy=2|3, strategicMode必填)
            1：强制
            2：非强制
    """
    _STRATEGIC_MODE_TYPE = (1, 2)

    def __init__(self, allocationPolicy: int, strategicMode: int = 1, maxVmCount: int = 5):
        self.allocationPolicy = allocationPolicy
        """
        maxVmCount: 最大vm数(allocationPolicy=4, maxVmCount必填)
        """
        self.maxVmCount = maxVmCount
        self.strategicMode = strategicMode
        if self.allocationPolicy not in self._PHYSICAL_ALLOCATION_POLICY_TYPE:
            raise KuberCloudException('params error', 'allocationPolicy is wrong')
        if self.allocationPolicy == 2 or self.allocationPolicy == 3:
            if self.strategicMode not in self._STRATEGIC_MODE_TYPE:
                raise KuberCloudException('params error', 'strategicMode is wrong')


class DiskBandwidth:
    """
        磁盘带宽类型
            1：总速度
            2：读写速度
    """
    _DISK_BANDWIDTH_TYPE = (1, 2)
    # 磁盘带宽读写速度单位
    _DISK_UNIT = ('MB/s', 'GB/s')
    # 磁盘带宽上行带宽|下行外带宽单位
    _NETWORK_UNIT = ('Mbps', 'Gbps')

    def __init__(self, diskBandwidthType: int,
                 # 磁盘带宽数值及单位
                 diskBandwidthNum: str = '', diskBandwidthUnit='MB/s',
                 # 磁盘带宽读取速度数值及单位
                 diskReadBandwidthNum: str = '', diskReadBandwidthUnit='MB/s',
                 # 磁盘带宽写入速度数值及单位
                 diskWriteBandwidthNum: str = '', diskWriteBandwidthUnit='MB/s',
                 # 磁盘带宽上行带宽数值及单位
                 networkUpBandwidthNum: str = '', networkUpBandwidthUnit='Mbps',
                 # 磁盘带宽下行带宽数值及单位
                 networkOutBandwidthNum: str = '', networkOutBandwidthUnit='Mbps'):
        numbs_dict = {'diskBandwidthNum': diskBandwidthNum,
                      'diskReadBandwidthNum': diskReadBandwidthNum,
                      'diskWriteBandwidthNum': diskWriteBandwidthNum,
                      'networkUpBandwidthNum': networkUpBandwidthNum,
                      'networkOutBandwidthNum': networkOutBandwidthNum}
        for numb_key, numb_value in numbs_dict.items():
            if not numb_value:
                continue
            try:
                int(numb_value)
            except ValueError:
                raise KuberCloudException('params error', '{} is wrong'.format(numb_key))

        self.diskBandwidthType = diskBandwidthType
        self.diskBandwidthNum = int(diskBandwidthNum) if diskBandwidthNum else diskBandwidthNum
        self.diskBandwidthUnit = diskBandwidthUnit
        self.diskReadBandwidthNum = int(diskReadBandwidthNum) if diskReadBandwidthNum else diskReadBandwidthNum
        self.diskReadBandwidthUnit = diskReadBandwidthUnit
        self.diskWriteBandwidthNum = int(diskWriteBandwidthNum) if diskWriteBandwidthNum else diskWriteBandwidthNum
        self.diskWriteBandwidthUnit = diskWriteBandwidthUnit
        self.networkUpBandwidthNum = int(networkUpBandwidthNum) if networkUpBandwidthNum else networkUpBandwidthNum
        self.networkUpBandwidthUnit = networkUpBandwidthUnit
        self.networkOutBandwidthNum = int(networkOutBandwidthNum) if networkOutBandwidthNum else networkOutBandwidthNum
        self.networkOutBandwidthUnit = networkOutBandwidthUnit

        if self.diskBandwidthType not in self._DISK_BANDWIDTH_TYPE:
            raise KuberCloudException('params error', 'diskBandwidthType is wrong')
        if self.diskBandwidthUnit not in self._DISK_UNIT or \
                self.diskReadBandwidthUnit not in self._DISK_UNIT or self.diskWriteBandwidthUnit not in self._DISK_UNIT:
            raise KuberCloudException('params error', 'unit should be "MB/s" or "GB/s"')
        if self.networkUpBandwidthUnit not in self._NETWORK_UNIT or \
                self.networkUpBandwidthUnit not in self._NETWORK_UNIT:
            raise KuberCloudException('params error', 'unit should be "Mbps" or "Gbps"')
